﻿namespace JavaScript8000.Scaner
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using JavaScript8000.Common;

    /// <summary>
    /// Represent results of scan.
    /// </summary>
    public class ScanResults
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ScanResults"/> class.
        /// </summary>
        /// <param name="tokens">
        /// The tokens.
        /// </param>
        /// <param name="errors">
        /// The errors.
        /// </param>
        /// <param name="userTokens">
        /// The user tokens.
        /// </param>
        public ScanResults(List<Token> tokens, List<ScanError> errors, UserTokens userTokens)
        {
            if (tokens == null || errors == null || userTokens == null)
            {
                throw new ArgumentNullException("All arguments should be not null");
            }

            this.Tokens = tokens;
            this.UserTokens = userTokens;
            this.Errors = errors;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the errors.
        /// </summary>
        /// <value>
        /// The errors.
        /// </value>
        public List<ScanError> Errors { get; private set; }

        /// <summary>
        /// Gets or sets the tokens.
        /// </summary>
        /// <value>
        /// The tokens.
        /// </value>
        public List<Token> Tokens { get; private set; }

        /// <summary>
        /// Gets or sets the user tokens.
        /// </summary>
        /// <value>
        /// The user tokens.
        /// </value>
        public UserTokens UserTokens { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the string of tokens list.
        /// </summary>
        /// <returns>
        /// The get string of tokens list.
        /// </returns>
        public string GetStringOfTokensList()
        {
            var sb = new StringBuilder();
            foreach (Token token in this.Tokens)
            {
                if (token.Code == SystemTokens.IdCode)
                {
                    sb.Append(
                        string.Format(
                            "{0}\t<ID>\t\t\t{1}\t{2}\r\n", 
                            token.Code, 
                            token.UserCode, 
                            this.UserTokens.GetByUserCode(token.UserCode)));
                }
                else if (token.Code == SystemTokens.NumericLiteralCode)
                {
                    sb.Append(
                        string.Format(
                            "{0}\t<NUMERIC LITERAL>\t{1}\t{2}\r\n", 
                            token.Code, 
                            token.UserCode, 
                            this.UserTokens.GetByUserCode(token.UserCode)));
                }
                else if (token.Code == SystemTokens.StringLiteralCode)
                {
                    sb.Append(
                        string.Format(
                            "{0}\t<STRING LITERAL>\t\t{1}\t{2}\r\n", 
                            token.Code, 
                            token.UserCode, 
                            this.UserTokens.GetByUserCode(token.UserCode)));
                }
                else if (token.Code == SystemTokens.BoolLiteralCode)
                {
                    sb.Append(
                        string.Format(
                            "{0}\t<BOOL LITERAL>\t\t{1}\t{2}\r\n",
                            token.Code,
                            token.UserCode,
                            this.UserTokens.GetByUserCode(token.UserCode)));
                }
                else if (token.Code == SystemTokens.BoolLiteralCode)
                {
                    sb.Append(
                        string.Format(
                            "{0}\t<NULL LITERAL>\t\t{1}\t{2}\r\n",
                            token.Code,
                            token.UserCode,
                            this.UserTokens.GetByUserCode(token.UserCode)));
                }
                else if (token.Code == SystemTokens.EofCode)
                {
                    sb.Append(string.Format("{0}\t<EOF>", token.Code));
                }
                else
                {
                    sb.Append(string.Format("{0}\t{1}\r\n", token.Code, SystemTokens.GetName(token.Code)));
                }
            }

            return sb.ToString();
        }

        #endregion
    }
}